﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Outlook = Microsoft.WindowsMobile.PocketOutlook;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace GSync
{
    public class OutlookMobileAppointmentsProvider : OutlookMobileDataProvider<Outlook.Appointment, Outlook.AppointmentCollection>
    {
        protected override Microsoft.WindowsMobile.PocketOutlook.AppointmentCollection ItemCollection
        {
            get { return Session.Appointments.Items; }
        }

        public override void DeleteEntry(InternalEntry entry)
        {
            Outlook.Appointment targerNativeEntry = FindNativeEntryByID(entry.ID);
            targerNativeEntry.Delete();
        }

        protected override InternalEntry ConvertToInternal(Microsoft.WindowsMobile.PocketOutlook.Appointment nativeEntry)
        {

                InternalAppointment appointment = new InternalAppointment();

                appointment.AllDayEvent = nativeEntry.AllDayEvent;


                if (nativeEntry.Properties.Contains("_ID"))
                    appointment.ID = nativeEntry.Properties["_ID"] as string;

                appointment.IsBusy = (nativeEntry.BusyStatus != Microsoft.WindowsMobile.PocketOutlook.BusyStatus.Free);
                appointment.Location = nativeEntry.Location;
                appointment.Notes = nativeEntry.Body;
                appointment.Subject = nativeEntry.Subject;


                appointment.Recurrence.IsRecurrent = nativeEntry.IsRecurring;
                //create single-occurence event
                if (!appointment.Recurrence.IsRecurrent)
                {
                    appointment.BeginDate = nativeEntry.Start;
                    appointment.EndDate = nativeEntry.End;
                }
                else
                {
                    appointment.CanSynchronize = false;
                    //throw new NotImplementedException("Sorry, but recurrent events aren't supported yet. Please wait for the next version of the programm.");

                    //appointment.Recurrence.IterationsCount = nativeEntry.RecurrencePattern.Occurrences;
                    //appointment.Recurrence.RecurrenceBegin = nativeEntry.RecurrencePattern.PatternStartDate.Date.Add(nativeEntry.RecurrencePattern.PatternStartTime.TimeOfDay);
                    //appointment.Recurrence.RecurrenceEnd = nativeEntry.RecurrencePattern.PatternEndDate.Date.Add(nativeEntry.RecurrencePattern.PatternEndTime.TimeOfDay);

                    //switch (nativeEntry.RecurrencePattern.RecurrenceType)
                    //{
                    //    case Microsoft.WindowsMobile.PocketOutlook.RecurrenceType.Daily:
                    //        appointment.Recurrence.RecurrenceType = RecurrenceType.Daily;
                    //        break;
                    //    case Microsoft.WindowsMobile.PocketOutlook.RecurrenceType.Monthly:
                    //    case Microsoft.WindowsMobile.PocketOutlook.RecurrenceType.MonthByNumber:
                    //        appointment.Recurrence.RecurrenceType = RecurrenceType.Monthly;

                    //        break;
                    //    case Microsoft.WindowsMobile.PocketOutlook.RecurrenceType.Weekly:
                    //        appointment.Recurrence.RecurrenceType = RecurrenceType.Weekly;

                    //        appointment.Recurrence.RecurrencePattern = new BitMask(7);
                    //        for( int i=1; i<=7;i++ )
                    //        {
                    //            appointment.Recurrence.RecurrencePattern[i] = ((int)Math.Pow(2, i-1) & (int)nativeEntry.RecurrencePattern.DaysOfWeekMask)!=0;
                    //        }
                    //        break;
                    //    case Microsoft.WindowsMobile.PocketOutlook.RecurrenceType.Yearly:
                    //        appointment.Recurrence.RecurrenceType = RecurrenceType.Yearly;
                    //        break;
                    //    default:
                    //        appointment.Recurrence.IsRecurrent = false;
                    //        break;
                    //} 


                }

                return appointment;
        }

        protected override void CopyToNative(InternalEntry entry, Microsoft.WindowsMobile.PocketOutlook.Appointment nativeEntry)
        {
            InternalAppointment appointment = entry as InternalAppointment;

            if (appointment == null)
            {
                throw new ArgumentException("Entry is null or entry type isn't appointment.");
            }

            if (nativeEntry == null)
            { 
                
            }

            if (!nativeEntry.Properties.Contains("_ID") || nativeEntry.Properties["_ID"] == null)
                nativeEntry.Properties.Add("_ID", typeof(string), Outlook.PimPropertyFlags.DoNotReplicate);
            nativeEntry.Properties["_ID"] = appointment.ID;


            nativeEntry.AllDayEvent = appointment.AllDayEvent;
            nativeEntry.Body = appointment.Notes;
            nativeEntry.BusyStatus = appointment.IsBusy ? Outlook.BusyStatus.Busy : Outlook.BusyStatus.Free;
            nativeEntry.Location = appointment.Location;
            nativeEntry.Subject = appointment.Subject;

            if (!appointment.Recurrence.IsRecurrent)
            {
                nativeEntry.Start = appointment.BeginDate;
                nativeEntry.End = appointment.EndDate;

            }
            else
            {
                throw new NotImplementedException("Sorry, but recurrent events aren't supported yet. Please wait for the next version of the programm."); 

            }


        }


        public override void AddEntry(InternalEntry entry)
        {
            Outlook.Appointment newNativeAppointment = Session.Appointments.Items.AddNew();

            CopyToNative(entry, newNativeAppointment);

            newNativeAppointment.Update();
        }
    }
}
